package cn.ly.spring4.bean;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.support.SimpleInstantiationStrategy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component("abc")
public class CacheProxyTest extends SimpleInstantiationStrategy implements BeanFactoryPostProcessor{
	
	public CacheProxyTest(){
		/*PropertyPlaceholderConfigurer p = null;
		p.postProcessBeanFactory(null);*/
		System.out.println("==========CacheProxyTest()==============");
	}
	
	@Transactional
	public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner) {
		BeanPostProcessor bp;
		System.out.println("===in instantiate() RootBeanDefinition===" + bd);
		bd.getBeanClass();
		//System.out.println("===in instantiate() beanName===" + beanName);
		//System.out.println("===in instantiate() BeanFactory===" + owner);
		return super.instantiate(bd, beanName, owner);
	}
	
	public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner,final Constructor<?> ctor, Object... args) {
		System.out.println("in instantiate 2");
		return super.instantiate(bd, beanName, owner, ctor, args);
	}
	
	public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner,Object factoryBean, final Method factoryMethod, Object... args) {
		System.out.println("in instantiate 3");
		return super.instantiate(bd, beanName, owner, factoryBean, factoryMethod, args);
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		if(beanFactory instanceof AbstractAutowireCapableBeanFactory){
			System.out.println("===in if===" + beanFactory);
			((AbstractAutowireCapableBeanFactory)beanFactory).setInstantiationStrategy(this);
		}
	}
	
}
